﻿using System;

class FrequentNumberInArray

            // Write a program that finds the most frequent number in an array. Example:
	        // {4, 1, 1, 4, 2, 3, 4, 4, 1, 2, 4, 9, 3}  4 (5 times)

{
    static void Main()
    {
        Console.Write("Enter the lenght of the array:");
        int arraySize = int.Parse(Console.ReadLine());
        int[] array = new int[arraySize];
        Console.WriteLine();
        
        Console.WriteLine("Enter the elements of the array:");
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = int.Parse(Console.ReadLine());
        }

        Array.Sort(array);
        int temp = 1;
        int tempBest = 1;
        int freqElement;
        int freqBest;

        freqElement = array[0];
        for ( int i = 0; i < array.Length; i += tempBest)
        {
            tempBest = 1;
            freqBest = array[i];
            for (int j = i + 1; j < arraySize; j++)
            {
                if (array[i] == array[j])
                {
                    tempBest++;
                }
            }
            if (tempBest > temp)
            {
                temp = tempBest;
                freqElement = freqBest;
            }
        }
        Console.WriteLine("The most frequent element is:" + freqElement);
    }
}

